package com.shaozi.nc.data.mongodao;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;
import com.shaozi.data.mongo.dao.SZMongoBaseDao;
import com.shaozi.nc.data.model.CompanyInfoModel;

@Repository("companyMDao")
public class CompanyInfoMDao extends SZMongoBaseDao<CompanyInfoModel> {

	@Autowired
	public CompanyInfoMDao(MongoTemplate shaoziTemplate) {
		super(shaoziTemplate);
	}

	public CompanyInfoModel getCompanyInfo(String companyId) {
		Criteria cri = new Criteria("companyId").is(companyId);
		cri.and("isDel").is(0);
		return getMongoTemplate().findOne(new Query(cri), CompanyInfoModel.class);
	}

	public List<CompanyInfoModel> getCompanyInfos() {
		Criteria cri = new Criteria("isDel").is(0);
		return getMongoTemplate().find(new Query(cri), CompanyInfoModel.class);
	}

	public void upsertCompanyInfo(CompanyInfoModel company) {
		Criteria cri = new Criteria("companyId").is(company.getCompanyId());

		Update up = new Update();
		up.set("companyId", company.getCompanyId());
		up.set("companyName", company.getCompanyName());
		up.set("companyDomain", company.getCompanyDomain());
		up.set("lastUpdateTime", company.getLastUpdateTime());
		up.setOnInsert("isDel", 0);

		getMongoTemplate().upsert(new Query(cri), up, CompanyInfoModel.class);
	}
}
